package com.xcity.game.npc;

import naga.x.App;
import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.TemplateService;
import naga.x.game.template.annotation.Excel;

/**
 * npc战队
 * @author yang.li
 *
 */
@Excel(value = "npc.xlsx", sheet = 1)
public class NpcTeamTemplate extends Template {

	/** boss id */
	protected int bossId;
	protected NpcTemplate boss;

	/** 小怪id */
	protected int[] npcIds;
	protected NpcTemplate[] npcs;

	public int getBossId() {
		return bossId;
	}

	public void setBossId(int bossId) {
		this.bossId = bossId;
		this.boss = null;
	}

	public int[] getNpcIds() {
		return npcIds;
	}

	public void setNpcIds(int[] npcIds) {
		this.npcIds = npcIds;
		this.npcs = null;
	}
	
	public void setBoss(NpcTemplate boss) {
		this.boss = boss;
	}
	
	public NpcTemplate getBoss() {
		return boss != null || bossId == 0 ? boss : (boss = App.getApp().getServiceManager().get(TemplateService.class).get(NpcTemplate.class, bossId));
	}
	
	public void setNpcs(NpcTemplate[] npcs) {
		this.npcs = npcs;
	}

	public NpcTemplate[] getNpcs() {
		if (npcs == null && npcIds != null) {
			npcs = new NpcTemplate[npcIds.length];
			TemplateService service = App.getApp().getServiceManager().get(TemplateService.class);
			for (int i = 0; i < npcIds.length; i++) {
				NpcTemplate npc = service.get(NpcTemplate.class, npcIds[i]);
				npcs[i] = npc;
			}
		}
		return npcs;
	}
	
	public NpcTemplate find(int id) {
		NpcTemplate boss = getBoss();
		if (boss != null && id == boss.getId()) {
			return boss;
		}
		NpcTemplate[] npcs = getNpcs();
		if (npcs != null) {
			for (NpcTemplate npc : npcs) {
				if (npc != null && id == npc.getId()) {
					return npc;
				}
			}
		}
		return null;
	}

	@Override
	public void validate() throws TemplateException {}
}
